package cn.edu.sjtu.se.dslab.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.edu.sjtu.se.dslab.action.bean.PageBean;
import cn.edu.sjtu.se.dslab.component.data.DataComponent;
import cn.edu.sjtu.se.dslab.dao.IDataComponentDao;
import cn.edu.sjtu.se.dslab.exception.DBException;
import cn.edu.sjtu.se.dslab.service.IDataComponentService;

@Service("dataComponentService")
@Transactional
public class DataComponentServiceImpl implements IDataComponentService {

	@Resource(name = "dataComponentDao")
	private IDataComponentDao dataComponentDao;

	@Override
	public boolean addDataComponent(DataComponent component) {
		// TODO Auto-generated method stub
		dataComponentDao.save(component);
		return true;
	}

	@Override
	public DataComponent getDataComponentById(long id) {
		// TODO Auto-generated method stub
		DataComponent dataComponent = dataComponentDao.queryById(id);
		// dataComponent.setChildList(null);
		// dataComponent.setDepends(null);
		return dataComponent;
	}

	@Override
	public boolean updateDataComponent(DataComponent component) {
		// TODO Auto-generated method stub
		dataComponentDao.update(component);
		return true;
	}

	@Override
	public PageBean<DataComponent> getPageBean(int pageNo) throws DBException {
		// TODO Auto-generated method stub
		return dataComponentDao.queryByPageNum(pageNo);
	}

	@Override
	public DataComponent getDataComponentByUri(String uri) {
		// TODO Auto-generated method stub
		// DataComponent dataComponent = dataComponentDao.queryByUri(uri);
		// if (dataComponent.getChildList() != null) {
		// Set<DataComponent> components = new HashSet<DataComponent>();
		// for (DataComponent data:dataComponent.getChildList()) {
		// DataComponent temp = getDataComponentById(data.getId());
		// components.add(temp);
		// }
		// dataComponent.setChildList(components);
		// }
		// if (dataComponent.getDepends() != null) {
		// Set<DataComponent> components = new HashSet<DataComponent>();
		// for (DataComponent data:dataComponent.getDepends()) {
		// DataComponent temp = getDataComponentById(data.getId());
		// components.add(temp);
		// }
		// dataComponent.setDepends(components);
		// }
		return dataComponentDao.queryByUri(uri);
	}

	@Override
	public List<DataComponent> getAllDataComponentList() {
		// TODO Auto-generated method stub
		return dataComponentDao.queryAll();
	}

}
